// zhouyuyue_yajin/pages/register/register.js
let app = getApp();
let http = require('../../util/http.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // school_list:[
    //   {      
    //     "id": 1,
    //      "name": "幼儿园1"
    //   }, {
    //     "id": 2,
    //     "name": "幼儿园2"        
    //   }
    // ],//学校列表
    school_list:[],
    is_show_picker:false,//是否显示学校选框
    school_choosed:'',//选择的学校
    school_id:'',//学校id
    user_name:'',//用户名
    phone:'',//手机号
  },

  // 注册
  register:function(){
    let that = this;
    if(that.data.user_name==''){
      wx.showToast({
        title: '请输入用户名',
        duration: 1000,
        icon: 'none',
      })
      return
    };
    if(that.data.phone==''){
      wx.showToast({
        title: '请输入手机号',
        duration: 1000,
        icon: 'none',
      })
      return
    };
    if(that.data.school_choosed==''){
      wx.showToast({
        title: '请选择学校',
        duration: 1000,
        icon: 'none',
      })
      return
    };
    http.post('realInfo',{
      realname:that.data.user_name,
      phone:that.data.phone,
      school:that.data.school_id
    },function(res){
      console.log('注册',res);
      if(res.data.errno==0){
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  },

  // 输入手机号
  inputPhone:function(e){
    this.setData({
      phone: e.detail.value
    });
  },

  // 输入用户名
  inputName:function(e){
    // console.log(e);
    this.setData({
      user_name: e.detail.value
    });
  },

  //获取学校列表
  getSchoolList:function(){
    let that = this;
    http.get('school',{},function(res){
      console.log('学校',res);
      that.setData({
        school_list: res.data.data
      });
    });
  },

  //选择学校
  chooseSchool:function(e){
    // console.log(e);
    let that = this;
    let i = e.detail.value[0];
    that.setData({
      school_choosed: that.data.school_list[i].name,
      school_id: that.data.school_list[i].id
    });
  },

  // 关闭学校选择框
  closePicker:function(){
    this.setData({
      is_show_picker:false
    });
  },

  //显示学校选择框
  showPicker:function(){
    let that = this;
    this.setData({
      is_show_picker: true,
      school_choosed: that.data.school_list[0].name,
      school_id: that.data.school_list[0].id
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getSchoolList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})